{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 需求\n",
    "\n",
    "import numbers\n",
    "class Field:\n",
    "    \n",
    "class IntField(Field):\n",
    "    def __init__(self, db_column, min_value=None, max_value=None):\n",
    "        self._value = None\n",
    "        self.min_value = min_value\n",
    "        self.max_value = max_value\n",
    "        self.db_column = db_column\n",
    "        \n",
    "        if min_value is not None:\n",
    "            if not isinstance(min_value, numbers.Integral):\n",
    "                raise ValueError(\"min_value must be int\")\n",
    "            elif min_value < 0:\n",
    "                raise ValueError(\"min_value must be positive int\")\n",
    "                \n",
    "        if max_value is not None:\n",
    "            if not isinstance(max_value, numbers.Integral):\n",
    "                raise ValueError(\"max_value must be int\")\n",
    "            elif max_value < 0:\n",
    "                raise ValueError(\"max_value must be positive int\")\n",
    "                \n",
    "        if min_value is not None and max_value is not None:\n",
    "            if min_value > max_value:\n",
    "                raise ValueError(\"min_value must be smaller than max_value\")\n",
    "        \n",
    "    def __get__(self, instance, owner):\n",
    "        return self._value\n",
    "\n",
    "    def __set__(self, instance, value):\n",
    "        if not isinstance(value, numbers.Integral):\n",
    "            raise ValueError('int value need')\n",
    "        if value < self.min_value or value > self.max_value:\n",
    "            raise ValueError('value must between min_value and max_value')\n",
    "        self._value = value\n",
    "\n",
    "class CharField(Field):\n",
    "    def __init__(self, db_column, max_length=None):\n",
    "        self.db_column = db_column\n",
    "        self._value = None\n",
    "\n",
    "        if max_length is None:\n",
    "            raise ValueError(\"you must specify max_length for CharField\")\n",
    "        self.max_length = max_length\n",
    "    \n",
    "    def __get__(self, instance, owner):\n",
    "        return self._value\n",
    "    \n",
    "    def __set__(self, instance, value):\n",
    "        if not isinstance(value, str):\n",
    "            raise ValueError('string value need')\n",
    "        if len(value) > self.max_length:\n",
    "            raise ValueError('value len beyond len of max_length')\n",
    "        self._value = value\n",
    "\n",
    "\n",
    "class ModelMetaClass(type):\n",
    "#     def __new__(cls, *args, **kwargs):\n",
    "    def __new__(cls, name, bases, attrs, **kwargs):\n",
    "        fields = {}\n",
    "        for key, value in attrs.items():\n",
    "            if isinstance(value, Field):\n",
    "                fields[key] = value\n",
    "        attrs_meta = attrs.get(\"Meta\", None)\n",
    "        _meta = {}\n",
    "        db_table = name.lower()\n",
    "        if attrs_meta is not None:\n",
    "            table = getattr(attrs_meta, \"db_table\", None)\n",
    "            if table is not None:\n",
    "                db_table = table\n",
    "        \n",
    "        _meta[\"db_table\"] = db_table\n",
    "        attrs[\"_meta\"] = _meta\n",
    "        attrs[\"fields\"] = fields\n",
    "        def attrs[\"Meta\"]\n",
    "        \n",
    "        return super().__new__(cls, name, bases, attrs, **kwargs)\n",
    "\n",
    "\n",
    "class User(metaclass=ModelMetaClass):\n",
    "    name = CharField(db_column=\"\", max_length=10)\n",
    "    age = IntField(db_column=\"\", min_value=0, max_value=100)\n",
    "    \n",
    "    class Meta:\n",
    "        db_table = \"user\"\n",
    "        \n",
    "if __name__ == \"__main__\":\n",
    "    user = User()\n",
    "    user.name"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
